rm(list = ls())
library(countrycode)
library(pscl)
library(foreign)
setwd("~/Desktop/QJPS Replication")

data <- read.csv("data/roll_call_matrix.csv")
data <- data[order(data$cowcode),]

##Miller et all democracy data
dem <- read.dta13("data/ccpcnc_v2_small.dta")

#combine democracy data with roll call matrix
data$match <- paste(data$cowcode, data$year)
dem$match <- paste(dem$cowcode, dem$year)
m <- match(data$match, dem$match)
table(is.na(m))

dem$country <- as.character(dem$country)
data$country_name <- dem$country[m]
data$country_name <- as.character(data$country_name)

#get rid of Mexico
data <- data[data$country_name != "Mexico",]

#subset data to only those that had a change occur
#some of the original data are missing because the CCP 
#authors haven't coded it yet. For example, the Bahamas only enter the datset in 2002, 
#but became a country in 1973.
data2 <- data[data$evnt == 1,]
data2 <- data2[,11:(ncol(data2)-3)]

#How many missing values are there for each entry
num.missing <- apply(data2, 1, function(x)sum(ifelse(is.na(x), 1, 0)))
prop.missing <- num.missing / ncol(data2)

#Get rid of those entries with more than 90 percent missing values
data3 <- data2[prop.missing < .9,]

#make the matrix a roll call object
mat <- rollcall(data3, yea = 1, nay = 0, legis.names = rownames(data3), vote.names = colnames(data3))

#Scale using WNOMINATE 10-dimensional model
set.seed(4227492)
idealpoints10 <- wnominate(mat, dims = 10, polarity = c(1,1,1,1,1,1,1,1,1,1))

#Figure A6-APRE with 10 dimensions
apre <- idealpoints10$fits[11:20]
x <- 1:10
plot(x, apre, ylim = c(0, 1), pch = 16, xlab = "Dimensions", ylab = "APRE", type = "l", main = "Aggregate Proportional Reduction in Error", axes = F)
points(x, apre, pch = 16)
axis(1, at = 1:10)
axis(2, at = seq(0, 1, .2))
box()